// Field Update Operators

// Use the $set operator to replace the value of a field to the specified value. 
// If the field does not exist, the $set operator will add the field with the specified value.
db.crud.update({"weight" : 704}, {"$set" : {"vampires" : 165}})

// update multiple documents
db.crud.update({"loves" : "apple"}, {"$set" : {"weight" : 600}}, {"multi" : true})

// upsert, creates a new document when no document matches the query criteria. 
db.crud.update({"weight" : 5000}, {"$set" : {"vampires" : 165}}, {"upsert" : true})

// The $unset operator deletes a particular field. 
// The specified value in the $unset expression (i.e. "" below) does not impact the operation.
db.crud.update({"weight" : 704}, {"$unset" : {"vampires" : ""}})

// The $inc operator increments a value of a field by a specified amount. If the field does not exist, $inc adds the field and sets the field to the specified amount.
// $inc accepts positive and negative incremental amounts.
db.crud.update({"weight" : 704}, {"$inc" : {"vampires" : 1}})

// Array Update Operators

// $addToSet operator adds a value to an array only if the value is not already in the array.
// no duplicate keys
db.crud.update({"weight" : 101}, {"$addToSet" : {"loves" : "banana"}})

// $each to add multiple values to the array field
db.crud.update(	{ "operation" : "addToSet" }, // criteria
                { "$addToSet": { "loves": { "$each": [ "banana","grape","apple" ] } } } ) // elements to add

// $pop operator removes the first or last element of an array. 
// -1 to remove the first element of an array and 1 to remove the last element in an array.
db.crud.update( { _id: 1 }, { $pop: { scores: -1 } } ) // removes first element
db.crud.update( { _id: 1 }, { $pop: { scores: 1 } } ) // removes last element
                
// $push operator appends a specified value to an array.
db.crud.update({"weight" : 540}, {"$push" : {"loves" : "apple"}})

// $pull operator removes from an existing array all instances of a value or values 
// that match a specified query.
db.crud.update({"weight" : 540}, {"$pull" : {"loves" : "apple"}})